﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleProject
{
    public class Virtual_Override_New
    {

        /// <summary>
        /// 测试方法
        /// </summary>
        public void Virtual_Override_Test()
        {
            //测试代码
            BaseClass bClass = new BaseClass();
            bClass.GetMessage();        //base
            BaseClass bs = new SubClass();
            bs.GetMessage();        //override
            BaseClass bsn = new SubClassNew();
            bsn.GetMessage();       //重写，调用base 中的方法

            SubClass sClass = new SubClass();
            sClass.GetMessage();

            SubClassNew sClassN = new SubClassNew();
            sClassN.GetMessage();

            BaseClass bsn1 = sClassN;
            bsn1.GetMessage();
        }
    }
    public class BaseClass
    {
        public BaseClass() { }

        public virtual void GetMessage()
        {
            Console.WriteLine("this is BaseClass");
        }
    }

    public class SubClass : BaseClass
    {
        public override void GetMessage()
        {
            Console.WriteLine("this is SubClass - override");
        }
    }

    public class SubClassNew : BaseClass
    {
        public new void GetMessage()
        {
            Console.WriteLine("this is SubClass - New");
        }
    }





    /**********************************************************/

    /*  测试代码 
    DerivedB b = new DerivedB();
    b.Invoke();      //2
    Console.WriteLine(b.x.ToString());      //2

    BaseA a = b;
    a.Invoke();         //1
    a.TrueValue = 3;
    a.Invoke(); //3
    Console.WriteLine(a.x.ToString());      //3
    Console.WriteLine(b.TrueValue.ToString());      //2

    Console.WriteLine("------");
    BaseA ba = new BaseA();
    ba.Invoke();        //1
    ba.TrueValue = 5;

    ba.Invoke();        //5
    Console.WriteLine(ba.TrueValue.ToString());     //5
     * **/
    public class BaseA
    {
        public int x = 1;
        public void Invoke()
        {
            Console.WriteLine("BaseA.x:" + x.ToString());
        }

        public int TrueValue
        {
            get { return x; }
            set { x = value; }
        }
    }

    public class DerivedB : BaseA
    {
        public int x = 2;
        public new void Invoke()
        {
            Console.WriteLine("DeriverdB.x:" + x.ToString());
        }
        public new int TrueValue
        {
            get { return x; }
            set { x = value; }
        }
    }
}
